/*
 * Created on Jun 04, 2004
  */
package com.scalar.chiptrack.sales.actions.quote;

import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.scalar.ScalarException;
import com.scalar.chiptrack.ChipTrackAction;
import com.scalar.chiptrack.alarms.Alarm;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.sales.Quotation;
import com.scalar.chiptrack.sales.dao.quote.QuotationDAO;
import com.scalar.chiptrack.users.User;


public class QuotationNotificationShowAction extends ChipTrackAction {

    private static final String ACTION_NAME = "/quotationNotificationShow.do" ;

	public ActionForward execute(ActionMapping mapping,
				 ActionForm form,
				 HttpServletRequest request,
				 HttpServletResponse response)
	throws Exception {
		
		Alarm alarm = (Alarm)request.getAttribute( "alarmObj" );
		HashMap errors = new HashMap();
		String salesOrderId = null;

        HttpSession session = request.getSession();

        User user = (User)session.getAttribute (SessionKeys.USER_KEY);


        if(user == null)
        {
	        session.setAttribute (SessionKeys.SOURCE_PAGE_KEY, ACTION_NAME + "?" + request.getQueryString());
            return (mapping.findForward ("login"));
        }


			Quotation quotation = null;
	
			// To load the Quotation Details and Line Item Details
	
			try{
			if( alarm != null && alarm.getId() > 0 ){

				request.setAttribute( "alarm" , alarm );

                String quotationId = QuotationDAO.getQuotationIdByAlarmId( Integer.toString( alarm.getId() ) );
                quotation = QuotationDAO.loadQuotation( quotationId, false );
                request.setAttribute( "quotation" , quotation );

            	ArrayList presentList = QuotationDAO.loadOrderLineItemsWithCustomerPartNo(quotation.getQuotationId(),quotation.getCustomerId());
				request.setAttribute( "presentList" , presentList );
			  }
			} catch ( ScalarException se ){
				
				errors.put ( "Error" , "Could not Load the Quotation Details" );
				request.setAttribute ( SessionKeys.ERROR_MESSAGES , errors );

				se.getRootCause().printStackTrace();
			}			
			
		return mapping.findForward( "success" );
	}

}
